package com.frogs.ucenter.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PropertyUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;

/**
 *专题提交页
 *@author hongyuhao
 */
@Controller
@RequestMapping("/zhuanti")
public class SpecialController extends BaseController3 {
	private final static Logger log = Logger.getLogger(SpecialController.class);
	private static final String DOMAIN_SCRIPT = "<script>document.domain = 'frogshealth.com';</script>";
	
	@RequestMapping("/{type}/commit")
    public void log(HttpServletRequest request, HttpServletResponse response, ModelMap model, @PathVariable String type) {
    	try {
    		response.setContentType("text/html;charset=utf-8");
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		
    		String tips = reqInfo.getParameter(ApiUrlParamEnum.tips.getParamName());
    		if(StringUtil.isEmpty(tips)) {
    			tips = "操作成功";
    		}
    		Logger newLog = createLogger(type);
    		String message = reqInfo.getParameter(ApiUrlParamEnum.name.getParamName()) + "," 
    				+ reqInfo.getParameter(ApiUrlParamEnum.sex.getParamName()) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.healthConditionTag.getParamName()) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.email.getParamName()) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.phone.getParamName()) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.address.getParamName()) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.postcode.getParamName()) + ","
    				+ reqInfo.getIntParameter(ApiUrlParamEnum.age.getParamName(), 0) + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.content.getParamName(), "") + ","
    				+ reqInfo.getParameter(ApiUrlParamEnum.remark.getParamName(), "")  + ","
    				+ DateUtil.formatDate(new Date(), "yyyy-MM-dd") ;
    		newLog.info(message);
    		response.getWriter().print(DOMAIN_SCRIPT + "{'result':1}");
		} catch (Exception e) {
			log.error("func[home] type[" + type + "] desc[fail]", e);
			try {
				response.getWriter().print(DOMAIN_SCRIPT + "{'result':0}");
			} catch (IOException e1) {
				log.error("func[home] error while record form");
			}
		}
    }
	
	/**
	 * 生成log4j配置，一天生成一个，明明为当天年月日
	 * 生成在日志文件主目录下zhuanti目录下，type为不同专题文件目录
	 * @param type
	 * @return
	 */
	private Logger createLogger(String type) {
		Logger newLog = Logger.getLogger(type);
		String logRoot = PropertyUtil.getProperty("customLogConfig", "log.custom.root");
		String logFileName = logRoot+"/zhuanti/" + type + "/" + DateUtil.getCurrentTime("yyyyMMdd") + ".csv";
		PatternLayout layout=new PatternLayout();
	    String format="%m%n";
	    layout.setConversionPattern(format);
	    
	    newLog.setAdditivity(false);
	    newLog.setLevel(Level.INFO);
	    
	    //create appender;
	    FileAppender appender=null;
	    try {
	      appender = (FileAppender) newLog.getAppender(type);
	      if(null == appender) {
	    	  appender=new FileAppender(layout,logFileName,true);
	    	  appender.setName(type);
	    	  newLog.addAppender(appender);
	      } else {
	    	  //设置文件指定文件
	    	  ((FileAppender)appender).setFile(logFileName, true, false, appender.getBufferSize());
	      }
	    }
	    catch (Exception ex) {
	    	
	    }
	    
		return newLog;
	}
	
}
